package com.forward.uploadapp.entity;

import android.content.Context;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ljh on 17-12-18.
 */

public class AppLayout {
    private int result;
    private String message;
    private List<AppLayoutInfo> appLayoutInfos;

    public AppLayout() {
    }

    public AppLayout(JSONObject obj, Context context) throws JSONException {
        result = obj.getInt("result");
        message = obj.getString("message");
        appLayoutInfos = new ArrayList<>();
        JSONArray arr = obj.getJSONArray("data");
        for (int i = 0; i < arr.length(); i++) {
            appLayoutInfos.add(new AppLayoutInfo(arr.getJSONObject(i)));
        }
    }

    public int getResult() {
        return result;
    }

    public void setResult(int result) {
        this.result = result;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public List<AppLayoutInfo> getAppLayoutInfos() {
        return appLayoutInfos;
    }

    public void setAppLayoutInfos(List<AppLayoutInfo> appLayoutInfos) {
        this.appLayoutInfos = appLayoutInfos;
    }

    @Override
    public String toString() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("{\"result\":\"").append(200).append("\",");
        buffer.append("\"message\":\"").append("ok").append("\",");
        buffer.append("\"data\":").append("[\n");
        if (appLayoutInfos.size() > 0) {
            for (int i = 0; i < appLayoutInfos.size(); i++) {
                buffer.append(appLayoutInfos.get(i).toString());
                if (i != appLayoutInfos.size() - 1) {
                    buffer.append(",");
                }
                buffer.append("\n\n");
            }
        }
        buffer.append("]}");
        return buffer.toString();
    }

    public static class AppLayoutInfo {
        private String type;
        private String whole;
        private List<AppInfo> appInfos;

        public AppLayoutInfo() {
        }

        public AppLayoutInfo(JSONObject obj) throws JSONException {
            type = obj.getString("type");
            whole = obj.getString("whole");
            JSONArray array = obj.getJSONArray("data");
            appInfos = new ArrayList<>();
            for (int i = 0; i < array.length(); i++) {
                appInfos.add(new AppInfo(array.getJSONObject(i)));
            }
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getWhole() {
            return whole;
        }

        public void setWhole(String whole) {
            this.whole = whole;
        }

        public List<AppInfo> getAppInfos() {
            return appInfos;
        }

        public void setAppInfos(List<AppInfo> appInfos) {
            this.appInfos = appInfos;
        }

        @Override
        public String toString() {
            StringBuffer buffer = new StringBuffer();
            buffer.append("{\"type\":\"").append(type).append("\",");
            buffer.append("\"whole\":\"").append(whole).append("\",");
            buffer.append("\"data\":").append("[");
            if (appInfos != null && appInfos.size() > 0) {
                for (int i = 0; i < appInfos.size(); i++) {
                    buffer.append(appInfos.get(i).toString());
                    if (i != appInfos.size() - 1) {
                        buffer.append(",");
                    }
                    buffer.append("\n\n");
                }
            }
            buffer.append("]}");
            return buffer.toString();
        }
    }
}
